<?php
	/**
	 * Created by PhpStorm.
	 * User: 陈森
	 * Date: 2020/12/30
	 * Time: 19:19
	 */

	namespace app\admin\controller\member;


	use app\admin\model\member\MemberModel;
	use core\basic\Controller;

	class MemberWithdrawController extends Controller
	{
		private $model;

		public function __construct()
		{
			$this->model = new MemberModel();
		}

		public function index()
		{
			$this->display('member/withdraw.html');
		}

		public function indexJson()
		{
			if ($_GET) {
				$page = get('page') ? get('page') : 1;
				$page = intval($page);
				$limit = get('limit') ? get('limit') : 1;
				$limit = intval($limit);
				$start = $limit * ($page - 1);
				$res = $this->model->getwithdrawInfo($start, $limit);
				$data = array(
					'code' => 0,
					'msg' => '',
					'count' => count($res),
					'data' => $res
				);
				return json_encode($data);
			}
		}

		public function consent()
		{
			if ($_GET) {
				$id = get('id');
				$usermobile = get('usermobile');
				$Getwithdraw = $this->model->Getwithdraw(['id' => $id, 'status' => 1]);
				if ($Getwithdraw) {
					$data = array(
						'status' => 2,
						'audit_time' => get_datetime(),
					);
					$Modres = $this->model->ModWithdraw(['id' => $id], $data);
					if ($Modres) {
						if ($this->model->Modfrost(['user_id' => $Getwithdraw->user_id, 'id' => $Getwithdraw->frost_id], ['thawing_time' => get_datetime(), 'is_frost' => 1])) {
							$this->log('同意用户 ' . $usermobile . '提现' . $Getwithdraw->quota . '扣除手续费' . $Getwithdraw->sxf . '实到金额' . $Getwithdraw->amount . '成功');
							$JunLog = '用户' . $usermobile . '申请提现' . $Getwithdraw->quota . '实际到账'.$Getwithdraw->amount.'同意,等待打款';
							model('admin.member.Bill')->AddBill($Getwithdraw->user_id, 7, $JunLog, 1);
							json(1, '同意打款');
						}
					}
				} else {
					json(0, '已经审核过');
				}

			}
		}

		public function refuse()
		{
			$id = get('id');
			$usermobile = get('usermobile');
			$Getwithdraw = $this->model->Getwithdraw(['id' => $id, 'status' => 1]);
			if ($Getwithdraw) {
				try {
					$data = array(
						'status' => 3,
						'audit_time' => get_datetime(),
					);
					$frostData = array(
						'is_frost' => 1,
						'thawing_time' => get_datetime(),
					);
					if ($this->model->Modfrost(['id' => $Getwithdraw->frost_id], $frostData) && $this->model->ModWithdraw(['id' => $id], $data) && $this->model->buysetInc($Getwithdraw->user_id, 'up_integral', $Getwithdraw->quota)) {
						$JunLog = '用户' . $usermobile . '申请提现' . $Getwithdraw->quota . '实际到账'.$Getwithdraw->amount.'被拒绝,返回'.$Getwithdraw->quota.'充值积分';
						model('admin.member.Bill')->AddBill($Getwithdraw->user_id, 7, $JunLog, 1);
						$this->log('拒绝用户'.$usermobile.'提现'.$Getwithdraw->quota.'拒绝');
						json(1, '审核成功');
					}else{
						$this->model->rollback();
						json(0, error());
					}
				} catch (Exception $e) {
					$this->model->rollback();
					json(0, error());
				}

			} else {
				json(0, '已经审核过');
			}
		}
	}